function samples = fmc106_demo_capture(bee4_addr, fpga_id)
%%
% Matlab下for语句的使用，if-else-end语句的使用
% 位异或函数的使用
%%
% FMC106_DEMO_CAPTURE  Download and analyze captured data from FMC106 demo
%
%   This function demonstrates how to download captured data from the
%   on-FPGA Block RAM, re-interpret fixed-point data in Matlab, and analyze
%   the output.
%
%   Usage of the function is:
%     samples = fmc101_demo_capture('192.168.0.100', 'A');
%
%   The arguments to the function are the address/hostname of the BEE4
%   system and ID of the FPGA running the FMC101 reference design,
%   respectively.  Valid FPGA IDs are 'A', 'B', 'C', or 'D'.  Please note
%   that there may not be any serial port terminals open on the BEE4
%   system, or else the underlying hardware operations will fail.

% Check arguments
switch nargin
    case 2
        if ~ischar(bee4_addr), error('BEE4 hostname must be a string'); end
        if ~ischar(fpga_id), error('FPGA ID must be a character'); end
    otherwise
        error('Wrong number of input arguments');
end

% Define system parameters
bram_depth = 2^14;
num_words = bram_depth; % num_words = bram_depth * mux_ratio / num_samples_per_bram_word
num_samples = num_words * 2;

% Capture one window of live data on the FPGA
fprintf('\n');
fprintf('Triggering data capture on BEE4 FPGA\n');
try
    bee4_reg_write(bee4_addr,fpga_id,'capture',1);
    bee4_reg_write(bee4_addr,fpga_id,'capture',0);
catch
    error('Error while accessing serial port');
end

% Download, split, and scale captured data from BRAM
fprintf('Downloading captured data\n');
try
    d1_d0 = bee4_bram_read(bee4_addr,fpga_id,'d1_d0',bram_depth);
    assignin('base','d1_d0',d1_d0);
    d3_d2 = bee4_bram_read(bee4_addr,fpga_id,'d3_d2',bram_depth);
    assignin('base','d3_d2',d3_d2);
    d5_d4 = bee4_bram_read(bee4_addr,fpga_id,'d5_d4',bram_depth);
    assignin('base','d5_d4',d5_d4);
    d7_d6 = bee4_bram_read(bee4_addr,fpga_id,'d7_d6',bram_depth);
    assignin('base','d7_d6',d7_d6);
catch
    error('Error while accessing serial port');
end

% d1_d0=evalin('base','d1_d0');
% d3_d2=evalin('base','d3_d2');
% d5_d4=evalin('base','d5_d4');
% d7_d6=evalin('base','d7_d6');
A_shorts = word2shorts(d1_d0, 1);
A_samples = uint16castfix(A_shorts,0,0); % uint16 - unscaled offset binary
B_shorts = word2shorts(d3_d2, 1);
B_samples = uint16castfix(B_shorts,0,0); % uint16 - unscaled offset binary
C_shorts = word2shorts(d5_d4, 1);
C_samples = uint16castfix(C_shorts,0,0); % uint16 - unscaled offset binary
D_shorts = word2shorts(d7_d6, 1);
D_samples = uint16castfix(D_shorts,0,0); % uint16 - unscaled offset binary
q_samples = zeros(num_samples,1);

% NOT operation on the 14th bit
for n= 1: num_samples
         A_samples(n) = bitxor(A_samples(n),8192);
         B_samples(n) = bitxor(B_samples(n),8192);
         C_samples(n) = bitxor(C_samples(n),8192);
         D_samples(n) = bitxor(D_samples(n),8192);
end

samples = [A_samples B_samples C_samples D_samples];


%
% Plot the results
%
for n = 1:4
    f = figure;
    set(f, 'Name', ['ADC ' num2str(n) ' data captured on ' datestr(now)]);
    
    subplot(2,1,1);
    plot(1:num_samples, samples(:,n));
    title('Time samples');
    subplot(2,1,2);
    
    w = (0.5 * (1 - cos(linspace(0,2*pi,num_samples))))'; % Hanning window
    %w = (0.42 - 0.5*cos(linspace(0,2*pi,num_samples)) + 0.08*cos(linspace(0,4*pi,num_samples)))'; % Blackman window
    %w = 1;
    fft_samples = 20*log10(abs(fft((samples(:,n).*w)+(q_samples.*w)*1i)));
    fft_samples_fs = fft_samples - max(fft_samples(10:end));
    freqs = linspace(0, 5e8/2, num_samples/2);
    plot(freqs, fft_samples_fs(1:end/2));
    title('Frequency spectrum');
end
